package com.ttxs.uaa.api.common;

import com.ttxs.common.core.vo.R;
import com.ttxs.uaa.api.BaseApi;
import com.ttxs.common.core.constant.Api;
import com.ttxs.uaa.service.ISysMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(Api.Common.Auth.PREFIX)
@RequiredArgsConstructor
public class AuthApi extends BaseApi {
    private final ISysMenuService sysMenuService;

    /**
     * 根据平台ID查询全部的权限菜单树，简化前端授权时显示树的代码
     * @return
     */
    @GetMapping
    public R getMenuTree() {
        return R.ok(sysMenuService.getMenuTree(getPlatformId()));
    }

    /**
     * 根据平台ID查询全部的权限菜单列表
     * @return
     */
    @GetMapping(Api.Common.Auth.LIST_ALL)
    public R getAllMenu() {
        return R.ok(sysMenuService.getMenus(getPlatformId()));
    }
    /**
     * 根据单个角色名字查询对应的权限菜单ID，简化前端代码，方便选中权限树
     * @return
     */
    @GetMapping(Api.Common.Auth.ROLE_MENUS)
    public R getCheckedKey(@PathVariable String roleName) {
        return R.ok(sysMenuService.getCheckedKey(getPlatformId(), roleName));
    }

    @PostMapping
    public R authorize(@RequestParam String roleName,
                       @RequestParam String checkedMenuIds) {
        sysMenuService.authorize(getPlatformId(), roleName, checkedMenuIds);
        return R.ok();
    }
}
